home *** CD-ROM | disk | FTP | other *** search
/ Learn Microsoft Visual Basic 6.0 Now / Learn Microsoft Visual Basic 6.0 Now (Microsoft Press)(X03-58607)(1998).ISO / media / chap09 / b09d010.cc2 < prev    next >
Text File  |  1998-06-07  |  4KB  |  84 lines

  1. 0, Sub procedures are often used to handle 
  2. 4, input in a program when information 
  3. 6, comes from two or more sources and needs to 
  4. 9, be put in the same format. In this 
  5. 11, demonstration, a Sub procedure named AddName 
  6. 14, prompts the user for input and formats 
  7. 17, the text, so that it can be displayed on 
  8. 19, multiple lines in a text box. The 
  9. 23, procedure saves me programming time because I 
  10. 25, can call it from two event procedures, 
  11. 27, each associated with a different text 
  12. 29, box, Sales and Marketing. Since this 
  13. 34, procedure is declared in a standard module, I 
  14. 36, only need to type it in one place. 
  15. 39, Here's how the program works. On the Sales 
  16. 41, side, if I click the Add Name button, the 
  17. 45, AddName procedure is called, and I'm 
  18. 48, asked to enter a Sales employee name. I can 
  19. 52, type Bart. And when I click OK, it's 
  20. 56, placed in the list box. Now, I can go to 
  21. 58, the Marketing side and click the name. 
  22. 61, And I can type a name on this side, 
  23. 63, Kieran. When I click OK, it's added to 
  24. 66, Marketing. On the Sales side, I can type 
  25. 71, another name, Christian, and back to the Add 
  26. 75, Name side. Now, in each case, the AddName 
  27. 82, procedure added the name to the list 
  28. 84, box, even though I clicked it on two 
  29. 86, different sides. So let's quit this program 
  30. 89, and take a look at the program code, 
  31. 91, especially the code module that contains the 
  32. 94, AddName procedure. In the first line of 
  33. 99, this procedure, I create a prompt 
  34. 102, string that uses the words "Enter a", the 
  35. 106, Team variable, and "employee". Now the Team 
  36. 111, variable comes from the TeamVariable 
  37. 115, procedure call, and that's passed from the 
  38. 118, calling procedure to the Sub procedure. 
  39. 122, And that team will either be Sales or 
  40. 124, Marketing. And in the second line, all 
  41. 127, that prompt string is just placed into an 
  42. 129, input box and put on the screen. And 
  43. 132, those are the things we saw when we were 
  44. 133, prompted to enter a name. That's what 
  45. 136, customizes it for the Sales or the Marketing 
  46. 139, side. This is one Sub procedure, but I 
  47. 142, was able to customize it with that Team 
  48. 145, variable that comes into the procedure. 
  49. 147, Now, the third line builds a wrap 
  50. 149, character with character 13 and character 10. 
  51. 153, And that's just a combination of two 
  52. 156, sort of invisible characters that will create a 
  53. 159, carriage return. And what we want to do 
  54. 161, is build our text box one line at a 
  55. 163, time, and it should include the name that's 
  56. 167, entered and then a carriage return. Now, 
  57. 170, that's all returned back in a variable 
  58. 172, called ReturnString, and that's sent back
  59. 175, from the AddName procedure back to the 
  60. 177, calling procedure. So, let's go back now to 
  61. 180, our main form and double-click one of 
  62. 183, the buttons. This time we'll click AddName 
  63. 186, and we'll open the Code window, and 
  64. 187, we'll be able to see both the Sales and 
  65. 189, Marketing event procedures. And here is 
  66. 192, where the AddName procedure is called. We 
  67. 196, have the name, AddName and then a call by 
  68. 200, value here and a call by reference. 
  69. 204, These are two procedure arguments that are 
  70. 208, sent. And the return value comes back in 
  71. 212, Sales position and then is added to my 
  72. 215, text box in the next line. The same is 
  73. 221, true in the event procedure below. 
  74. 223, Marketing is sent so that the input box says 
  75. 226, Marketing and we know that that's a 
  76. 228, marketing name that we're going to add. Then, 
  77. 230, the Marketing position variable is 
  78. 232, returned because that's passed by reference. 
  79. 236, And then it is added to the text box in 
  80. 238, the line below. So, with two separate 
  81. 241, procedure calls, I'm able to call one 
  82. 244, procedure and customize it, and save myself 
  83. 246, lots of typing.
  84. 248, END